package com.ilop.siteraudio.data.manage;

import android.content.Context;

import com.ilop.siteraudio.greendao.DaoSession;


/**
 * greenDao
 */
public class DaoManager {

    private static final String TAG = DaoManager.class.getSimpleName();

    //多线程中要被共享的使用volatile关键字修饰
    private volatile static DaoManager manager = null;
    private static DaoSession sDaoSession;

    /**
     * 单例模式获得操作数据库对象
     */
    public static DaoManager getInstance() {
        if (manager == null){
            synchronized (DBDaoMaster.class){
                if (manager == null){
                    manager = new DaoManager();
                }
            }
        }
        return manager;
    }

    public void init(Context context) {
        String DB_NAME = "SiterAudio.db";
        DBDaoMaster.OpenHelper sHelper = new DBDaoMaster.OpenHelper(context, DB_NAME);
        DBDaoMaster sDaoMaster = new DBDaoMaster(sHelper.getWritableDb());
        sDaoSession = sDaoMaster.newSession();
    }

    /**
     * 完成对数据库的添加、删除、修改、查询操作，仅仅是一个接口
     */
    public DaoSession getDaoSession() {
        return sDaoSession;
    }

    /**
     * 清空数据库
     */
    public void deleteAllSQL(){
        DBDaoMaster.dropAllTables(sDaoSession.getDatabase(),true);
        DBDaoMaster.createAllTables(sDaoSession.getDatabase(), true);
    }
}
